<!-- author: xuett -->
<!-- date: 2025-05-09 17:35:47 -->
<!-- description:  -->
<template>
  <div class="audio-scanner">
    <h1>音频文件扫描器</h1>
    <button @click="selectAndScanDirectory" class="scan-button">
      <span v-if="!loading">选择目录并扫描</span>
      <span v-else class="loading-text"> <span class="spinner">⏳</span> 扫描中... </span>
    </button>

    <div v-if="selectedDirectory" class="directory-info">
      当前目录: <strong>{{ selectedDirectory }}</strong>
    </div>

    <div v-if="error" class="error-message">❌ {{ error }}</div>

    <div v-if="audioFiles.length > 0" class="results">
      <h2>找到 {{ audioFiles.length }} 个音频文件</h2>
      <div class="file-list">
        <div v-for="file in audioFiles" :key="file.path" class="file-item">
          <div class="file-name">{{ file.name }}</div>
          <div class="file-meta">
            <span class="file-extension">{{ file.extension }}</span>
            <button @click="playAudio(file.path)" class="play-button">▶ 播放</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'

const audioFiles = ref([])
const loading = ref(false)
const error = ref('')
const selectedDirectory = ref('')

const selectAndScanDirectory = async () => {
  try {
    loading.value = true
    error.value = ''
    audioFiles.value = []

    // 1. 打开目录选择对话框
    const dirPath = await window.api.openDirectoryDialog()
    if (!dirPath) {
      loading.value = false
      return
    }
    console.log('dirPath', dirPath)

    selectedDirectory.value = dirPath

    // 2. 扫描选定目录中的音频文件
    const result = await window.api.scanAudioFiles(dirPath)

    if (result.success) {
      audioFiles.value = result.files
    } else {
      error.value = `扫描失败: ${result.error}`
    }
  } catch (err) {
    error.value = `发生错误: ${err.message}`
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.audio-scanner {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  font-family: Arial, sans-serif;
}

.scan-button {
  background-color: #4caf50;
  color: white;
  border: none;
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.scan-button:hover {
  background-color: #45a049;
}

.loading-text {
  display: flex;
  align-items: center;
  gap: 8px;
}

.spinner {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.directory-info {
  margin: 15px 0;
  padding: 10px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.error-message {
  color: #d32f2f;
  background-color: #fde0e0;
  padding: 10px;
  border-radius: 4px;
  margin: 15px 0;
}

.results {
  margin-top: 20px;
}

.file-list {
  margin-top: 10px;
}

.file-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  border-bottom: 1px solid #eee;
}

.file-item:hover {
  background-color: #f9f9f9;
}

.file-name {
  flex-grow: 1;
}

.file-meta {
  display: flex;
  align-items: center;
  gap: 15px;
}

.file-extension {
  color: #666;
  font-size: 0.9em;
}

.play-button {
  background-color: #2196f3;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 3px;
  cursor: pointer;
}

.play-button:hover {
  background-color: #0b7dda;
}

.audio-player {
  width: 100%;
  margin-top: 20px;
}
</style>
